package com.wcl.demo.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.wcl.demo.R;
import com.wcl.demo.ui.adapter.ChosePersonAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/21.
 */
public class ChosePersonActivity extends Activity implements View.OnClickListener {


    private ListView lv;
    private ChosePersonAdapter mAdapter;
    private ArrayList<String> list = new ArrayList<String>();
    private Button bt_selectall;
    private Button bt_cancel;
    private Button bt_deselectall;

    private Button chaxun;

    private String wayKey;
    private String ifOpen;

    private boolean allowInvite=false;

    private int checkNum; // 记录选中的条目数量
    private TextView tv_show;// 用于显示选中的条目数量
    private TextView chose_person_save_textBtn;
    private ImageButton activity_chose_person_bac_Iv;
    private String groupName, desc;

    private List<String> memberlist = new ArrayList<String>();
    private List<String> usernames;

    {
        try {
            usernames = EMChatManager.getInstance().getContactUserNames();
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chose_person);

        groupName = getIntent().getStringExtra("groupName");
        desc = getIntent().getStringExtra("desc");

        wayKey = getIntent().getStringExtra("wayKey");
        ifOpen = getIntent().getStringExtra("ifOpen");

        initView();
        initData();
        //----------------
        // 实例化自定义的MyAdapter
        mAdapter = new ChosePersonAdapter(list, this);
        // 绑定Adapter
        lv.setAdapter(mAdapter);

        // 全选按钮的回调接口
        bt_selectall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 遍历list的长度，将MyAdapter中的map值全部设为true
                for (int i = 0; i < list.size(); i++) {
                    ChosePersonAdapter.getIsSelected().put(i, true);
                }
                // 数量设为list的长度
                checkNum = list.size();
                // 刷新listview和TextView的显示
                dataChanged();
            }
        });
        // 反选按钮的回调接口
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 遍历list的长度，将已选的设为未选，未选的设为已选
                for (int i = 0; i < list.size(); i++) {
                    if (ChosePersonAdapter.getIsSelected().get(i)) {
                        ChosePersonAdapter.getIsSelected().put(i, false);
                        checkNum--;
                    } else {
                        ChosePersonAdapter.getIsSelected().put(i, true);
                        checkNum++;
                    }
                }
                // 刷新listview和TextView的显示
                dataChanged();
            }
        });
        // 取消按钮的回调接口
        bt_deselectall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 遍历list的长度，将已选的按钮设为未选
                for (int i = 0; i < list.size(); i++) {
                    if (ChosePersonAdapter.getIsSelected().get(i)) {
                        ChosePersonAdapter.getIsSelected().put(i, false);
                        checkNum--;// 数量减1
                    }
                }
                // 刷新listview和TextView的显示
                dataChanged();
            }
        });

        // 绑定listView的监听器
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                // 取得ViewHolder对象，这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
                ChosePersonAdapter.ViewHolder holder = (ChosePersonAdapter.ViewHolder) arg1.getTag();
                // 改变CheckBox的状态
                holder.cb.toggle();
                // 将CheckBox的选中状况记录下来
                ChosePersonAdapter.getIsSelected().put(arg2, holder.cb.isChecked());
                // 调整选定条目
                if (holder.cb.isChecked() == true) {
                    checkNum++;
                } else {
                    checkNum--;
                }
                // 用TextView显示
                tv_show.setText("已选中" + checkNum + "项");
            }
        });
        //------------------

    }

    private void initData() {
        for (int i = 0; i < usernames.size(); i++) {
            list.add(usernames.get(i));
        }
    }

    // 刷新listview和TextView的显示
    private void dataChanged() {
        // 通知listView刷新
        mAdapter.notifyDataSetChanged();
        // TextView显示最新的选中数目
        tv_show.setText("已选中" + checkNum + "项");
    }



    private void initView() {
        chose_person_save_textBtn = (TextView) findViewById(R.id.chose_person_save_textBtn);
        chose_person_save_textBtn.setOnClickListener(this);


        activity_chose_person_bac_Iv = (ImageButton) findViewById(R.id.activity_chose_person_bac_Iv);
        activity_chose_person_bac_Iv.setOnClickListener(this);

        //--------------
        lv = (ListView) findViewById(R.id.lv);
        bt_selectall = (Button) findViewById(R.id.bt_selectall);
        bt_cancel = (Button) findViewById(R.id.bt_cancleselectall);
        bt_deselectall = (Button) findViewById(R.id.bt_deselectall);
        tv_show = (TextView) findViewById(R.id.tv);
        list = new ArrayList<String>();

        chaxun = (Button) findViewById(R.id.chaxun);
        chaxun.setOnClickListener(this);

        //---------------
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chose_person_save_textBtn:
                switch (wayKey) {
                    case "1"://公共群
                        try {
                            searchSelect();
                            EMGroupManager.getInstance().createPublicGroup(groupName, desc, members, false);
                        } catch (EaseMobException e) {
                            e.printStackTrace();
                        }

                        break;
                    case "2"://私有群
                        allowInvite=(ifOpen.equals("true")?true:false);
                        try {
                            searchSelect();
                            EMGroupManager.getInstance().createPrivateGroup(groupName, desc, members, allowInvite);
                        } catch (EaseMobException e) {
                            e.printStackTrace();
                        }

                        break;

                }

                finish();

                break;
            case R.id.activity_chose_person_bac_Iv:
                finish();
                break;
            case R.id.chaxun:
                searchSelect();
                break;

        }
    }

    int count = -1;

    String[] members;

    private void searchSelect() {
        List<String> newList = null;
        for (int i = 0; i < list.size(); i++) {
            if (ChosePersonAdapter.getIsSelected().get(i)) {
                count++;
                memberlist.add(list.get(i));

            }
        }
        members = new String[memberlist.size()];
        for (int i = 0; i < members.length; i++) {
            members[i] = memberlist.get(i);
            Log.i("haha", members[i] + "****");
        }
    }
}
